package com.cloudeasy.post.controller;

import com.cloudeasy.api.enums.ResultCodeEnum;
import com.cloudeasy.api.exception.CloudEasyException;
import com.cloudeasy.api.util.ResultUtil;
import com.cloudeasy.api.vo.Result;
import com.cloudeasy.post.client.UserClient;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.util.List;

@CrossOrigin
public class BaseController {
    private final UserClient userClient;

    public BaseController(UserClient userClient) {
        this.userClient = userClient;
    }

    /**
     * 逻辑校验
     *
     * @param permissionId  权限Id
     * @param areas         区域列表
     * @param operateUserId 操作用户Id
     * @return {@link Result<Boolean>}
     */
    protected Result<Boolean> getBooleanResult(String permissionId, List<String> areas, String operateUserId) {
        Result<Boolean> result = userClient.canDo(permissionId, areas, operateUserId);
        if (result.getCode().equals(ResultCodeEnum.SUCCESS.getCode())) {
            if (!result.getData()) {
                throw new CloudEasyException(ResultCodeEnum.NO_PERMISSION_OPERATE);
            }
        } else {
            return result;
        }
        return ResultUtil.success(true);
    }
}
